/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.contract.app;

import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.contract.dto.ContractOrderCloseDTO;
import com.pig4cloud.pigx.contract.dto.ContractOrderCloseMarketDTO;
import com.pig4cloud.pigx.contract.dto.ContractOrderOpenDTO;
import com.pig4cloud.pigx.contract.service.ContractOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * 订单表
 *
 * @since 2022-08-11 10:08:11
 */
@RestController
@RequestMapping("/app/order")
@Api(value = "appOrder", tags = "app订单管理")
@AllArgsConstructor
public class AppContractOrderController {

    private final ContractOrderService contractOrderService;

    /**
     * 开仓
     *
     * @param dto dto
     * @return {@link R}
     * @since 2022/8/16 18:07
     */
    @ApiOperation(value = "开仓", notes = "开仓")
    @SysLog("开仓")
    @PostMapping("/open")
//	@PreAuthorize("@pms.hasPermission('contract_contractorder_add')")
    public R open(@Validated @RequestBody ContractOrderOpenDTO dto) {
        contractOrderService.open(dto);
        return R.ok();
    }

    /**
     * 平仓
     *
     * @param dto dto
     * @return {@link R}
     * @since 2022/8/16 18:07
     */
    @ApiOperation(value = "平仓", notes = "平仓")
    @SysLog("平仓")
    @PostMapping("/close")
//	@PreAuthorize("@pms.hasPermission('contract_contractorder_add')")
    public R close(@Validated @RequestBody ContractOrderCloseDTO dto) {
        contractOrderService.close(dto);
        return R.ok();
    }

    /**
     * 市价全平
     *
     * @return {@link R}
     * @since 2022/8/16 18:07
     */
    @ApiOperation(value = "市价全平", notes = "市价全平")
    @SysLog("市价全平")
    @PostMapping("/close/market")
//	@PreAuthorize("@pms.hasPermission('contract_contractorder_add')")
    public R closeMarket(@Validated @RequestBody ContractOrderCloseMarketDTO dto) {
        contractOrderService.closeMarket(dto);
        return R.ok();
    }

    /**
     * 一键平仓
     *
     * @return {@link R}
     * @since 2022/8/16 18:07
     */
    @ApiOperation(value = "一键平仓", notes = "一键平仓")
    @SysLog("一键平仓")
    @PostMapping("/close/all")
//	@PreAuthorize("@pms.hasPermission('contract_contractorder_add')")
    public R closeAll() {
        contractOrderService.closeAll();
        return R.ok();
    }

    /**
     * 撤销单个订单
     *
     * @param id id
     * @return {@link R}
     * @since 2022/8/25 11:24
     */
    @ApiOperation(value = "撤销单个订单", notes = "撤销单个订单")
    @SysLog("撤销单个订单")
    @PostMapping("/cancel")
//	@PreAuthorize("@pms.hasPermission('contract_contractorder_cancel')")
    public R cancel(@Validated @RequestBody @NotNull(message = "数据异常，id为空") Long id) {
        contractOrderService.cancel(id);
        return R.ok();
    }

    /**
     * 撤销当前用户全部订单
     *
     * @return {@link R}
     * @since 2022/8/25 11:24
     */
    @ApiOperation(value = "全部撤销", notes = "全部撤销")
    @SysLog("全部撤销")
    @PostMapping("/cancel/all")
//	@PreAuthorize("@pms.hasPermission('contract_contractorder_cancel')")
    public R cancelAll() {
        contractOrderService.cancelAll();
        return R.ok();
    }
}
